package org.erp.spu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.erp.common.page.PageQueryModel;
import org.erp.spu.entity.SpuModel;
import org.erp.spu.entity.SpuModelTemplate;
import org.erp.spu.entity.bo.SpuModelBo;
import org.erp.spu.entity.dto.SpuModelDto;
import org.erp.spu.entity.vo.SpuModelVo;
import org.erp.spu.mapper.SpuModelMapper;
import org.erp.spu.mapper.SpuModelTemplateMapper;
import org.erp.spu.service.SpuModelService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 型号表(SpuModel)表服务实现类
 *
 * @author dingding
 * @since 2025-11-06 09:53:28
 */
@Service("spuModelService")
public class SpuModelServiceImpl extends ServiceImpl<SpuModelMapper, SpuModel> implements SpuModelService {

    @Resource
    private SpuModelMapper spuModelMapper;
    @Resource
    private SpuModelTemplateMapper spuModelTemplateMapper;

    @Override
    public List<SpuModelVo> getList(SpuModelBo spuModelBo) {
        LambdaQueryWrapper<SpuModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(spuModelBo.getModelName()), SpuModel::getModelName, spuModelBo.getModelName());
        queryWrapper.eq(spuModelBo.getSeriesId() != null, SpuModel::getSeriesId, spuModelBo.getSeriesId());
        queryWrapper.eq(spuModelBo.getBrandId() != null, SpuModel::getBrandId, spuModelBo.getBrandId());
        queryWrapper.eq(spuModelBo.getCategoryId() != null, SpuModel::getCategoryId, spuModelBo.getCategoryId());
        queryWrapper.eq(SpuModel::getDelFlag, 0);

        List<SpuModel> modelList = spuModelMapper.selectList(queryWrapper);
        return modelList.stream()
                .map(this::convertToVo)
                .collect(Collectors.toList());
    }

    @Override
    public PageQueryModel<SpuModelVo> getListByPage(SpuModelDto spuModelDto) {
        LambdaQueryWrapper<SpuModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(spuModelDto.getModelName()), SpuModel::getModelName, spuModelDto.getModelName());
        queryWrapper.eq(spuModelDto.getSeriesId() != null, SpuModel::getSeriesId, spuModelDto.getSeriesId());
        queryWrapper.eq(spuModelDto.getBrandId() != null, SpuModel::getBrandId, spuModelDto.getBrandId());
        queryWrapper.eq(spuModelDto.getCategoryId() != null, SpuModel::getCategoryId, spuModelDto.getCategoryId());
        queryWrapper.eq(SpuModel::getDelFlag, 0);
        Page<SpuModel> page = this.page(spuModelDto.buildPage(), queryWrapper);
        List<SpuModelVo> voList = page.getRecords().stream()
                .map(this::convertToVo)
                .collect(Collectors.toList());
        return PageQueryModel.of(page.getPages(), voList);
    }

    @Override
    public boolean addSpuModel(SpuModelDto spuModelDto) {
        Assert.isTrue(!checkModelName(spuModelDto), "型号名称已存在");
        SpuModel spuModel = new SpuModel();
        BeanUtils.copyProperties(spuModelDto, spuModel);
        if(spuModelMapper.insert(spuModel) >0){
            SpuModelTemplate spuModelTemplate = new SpuModelTemplate();
             spuModelTemplate.setTemplateName(spuModelDto.getTemplateName());
             spuModelTemplate.setModelId(spuModel.getId());
             spuModelTemplate.setModelName(spuModel.getModelName());
             spuModelTemplate.setTemplateId(spuModelDto.getTemplateId());
             spuModelTemplateMapper.insert(spuModelTemplate);
             return true;
        }
        return false;
    }

    @Override
    public boolean updateSpuModel(SpuModelDto spuModelDto) {
        Assert.isTrue(spuModelDto.getId() != null, "型号ID不能为空");
        SpuModel existingModel = spuModelMapper.selectById(spuModelDto.getId());
        Assert.isTrue(existingModel != null, "型号不存在");
        Assert.isTrue(!checkModelName(spuModelDto), "型号名称已存在");
        BeanUtils.copyProperties(spuModelDto, existingModel);
        SpuModelTemplate existingTemplate = spuModelTemplateMapper.selectOne(new LambdaQueryWrapper<SpuModelTemplate>().eq(SpuModelTemplate::getModelId, spuModelDto.getId()).eq(SpuModelTemplate::getDelFlag, 0));
        if(existingTemplate != null){
            existingTemplate.setTemplateName(spuModelDto.getTemplateName());
            existingTemplate.setTemplateId(spuModelDto.getTemplateId());
            spuModelTemplateMapper.updateById(existingTemplate);
        }else if(existingTemplate==null&&spuModelDto.getTemplateId() != null){
            SpuModelTemplate spuModelTemplate = new SpuModelTemplate();
            spuModelTemplate.setTemplateName(spuModelDto.getTemplateName());
            spuModelTemplate.setModelId(spuModelDto.getId());
            spuModelTemplate.setModelName(spuModelDto.getModelName());
            spuModelTemplate.setTemplateId(spuModelDto.getTemplateId());
            spuModelTemplateMapper.insert(spuModelTemplate);
        }

        return spuModelMapper.updateById(existingModel) > 0;
    }

    @Override
    public SpuModelVo getSpuModelVoById(Long id) {
        SpuModel spuModel = spuModelMapper.selectById(id);
        SpuModelVo  vo= convertToVo(spuModel);
        SpuModelTemplate spuModelTemplate = spuModelTemplateMapper.selectOne(new LambdaQueryWrapper<SpuModelTemplate>().eq(SpuModelTemplate::getModelId, id).eq(SpuModelTemplate::getDelFlag, 0));
         if(spuModelTemplate != null){
             vo.setTemplateId(spuModelTemplate.getTemplateId());
             vo.setTemplateName(spuModelTemplate.getTemplateName());
         }

        return vo ;
    }

    /**
     * 转换为VO对象
     *
     * @param spuModel
     * @return
     */
    private SpuModelVo convertToVo(SpuModel spuModel) {
        if (spuModel == null) {
            return null;
        }
        SpuModelVo vo = new SpuModelVo();
        BeanUtils.copyProperties(spuModel, vo);
        return vo;
    }

    /**
     * 检查型号名称是否已存在
     *
     * @param spuModelDto
     * @return
     */
    private boolean checkModelName(SpuModelDto spuModelDto) {
        if (!StringUtils.hasText(spuModelDto.getModelName())) {
            return false;
        }
        LambdaQueryWrapper<SpuModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpuModel::getModelName, spuModelDto.getModelName());
        queryWrapper.eq(SpuModel::getDelFlag, 0);
        if (spuModelDto.getId() != null) {
            queryWrapper.ne(SpuModel::getId, spuModelDto.getId());
        }
        return spuModelMapper.selectCount(queryWrapper) > 0;
    }
}
